package logic

import (
	"context"
	"fmt"
	"math/rand"
	"time"
	"wechat1/models/mysql"

	"wechat1/wechat1rpc/internal/svc"
	"wechat1/wechat1rpc/wechat1rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RegisterLogic) Register(in *wechat1rpc.RegisterRequest) (*wechat1rpc.RegisterResponse, error) {
	// todo: add your logic here and delete this line
	var FindWechat mysql.Wechat
	mysql.Db.Debug().Where("mobile", in.Mobile).Find(&FindWechat)
	if FindWechat.ID != 0 {
		return nil, fmt.Errorf("用户已注册，请重新输入")
	}
	Number := rand.Intn(90000) + 10000
	wechatName := fmt.Sprintf("lzk%d%s", Number, time.Now().Format("20060102150405"))
	wechat := mysql.Wechat{
		Mobile:     in.Mobile,
		Password:   in.Password,
		Avatar:     in.Avatar,
		Nickname:   in.Nickname,
		Address:    in.Address,
		WechatName: wechatName,
	}
	mysql.Db.Debug().Create(&wechat)
	return &wechat1rpc.RegisterResponse{}, nil
}
